// 1、无头单向非循环链表实现
public class SingleLinkedList {


   static class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    //头插法
    public void addFirst(int data) {
        /*if(head == null){
            head = new ListNode(data);
            return;
        }*/
        ListNode cur = new ListNode(data);
        cur.next = head;
        head = cur;
    }
    //尾插法
    public void addLast(int data){
        if(head == null){
            head = new ListNode(data);
        }
        ListNode cur = head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = new ListNode(data);
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) throws IndexIllegalException{
        try{
            if(index < 0 || index > size()){
                throw new IndexIllegalException("位置不合法");
            } else if (index == 0) {
                addFirst(data);
                return true;
            } else if (index == size()) {
                addLast(data);
                return true;
            }else{
                ListNode cur = head;
                ListNode pre = head;
                ListNode newNode = new ListNode(data);

                for (int i = 0; i < index-1; i++) {
                    cur = cur.next;
                    pre = pre.next;
                }
                newNode.next = cur.next;
                pre.next = cur;
                return true;
            }


        }catch(IndexIllegalException e){
            e.printStackTrace();
            return false;
        }
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = head;
        for (int i = 0; i < size(); i++) {
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head == null){
            return;
        } else if (head .val == key) {
            head = head.next;
            return;
        }
        ListNode cur = head.next;
        ListNode pre = head;

        while(cur != null){
            if(cur.val == key){
                pre.next = cur.next;
                return;
            }
            cur = cur.next;
            pre = pre.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null){
            return;
        }
        ListNode cur = head.next;
        ListNode pre = head;

        while(cur != null){
            if(cur.val == key){
                pre.next = cur.next;
                cur = cur.next;

            }else {
                cur = cur.next;
                pre = pre.next;
            }
        }
        if(head.val == key){
            head = head.next;
        }
    }

    //得到单链表的长度
    public int size(){
        ListNode cur = head;
        int size = 0;
        while(cur != null){
            cur = cur.next;
            size++;
        }
        return size;
    }
    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
    }
    public void clear(){
        ListNode cur = head;
        while (cur != null){
            head = cur.next;
            cur = null;
            cur = head;
        }
    }
}